import express from 'express';
import routes from './routers/routes.js';
import {AppDataSource} from './db/config/db.js'
import {User} from './db/entity/user.entity.js'
import {Todo} from './db/entity/todo.entity.js'

const app: express.Application = express();
const port = 3000;

// 使用路由
app.use(routes);

// 错误处理中间件
app.use((err: Error, req: express.Request, res: express.Response, next: express.NextFunction) => {
    console.error('Error caught by middleware:', err);
    res.status(500).send('Internal Server Error');
});

app.listen(port, async () => {
    AppDataSource.setOptions({
        entities: [User,Todo]
    })
    await AppDataSource.initialize()
    console.log("typeorm initialize success")
    console.log(`Server is running on port ${port}`);
});
